package cn.ai.boot.config.PCF;

import cn.ai.boot.PcmDataFieldConstant;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * @author jie.zhao@dms365.com
 * @Date 2025/7/18 16:40
 */
public class Bppc0405DataSyncServiceImplTEst {

    public static void main2(String[] args) {

        Map<String, List<Map<String, Object>>> temp = new HashMap<>();
        Map<String, Object> a1 = new HashMap<>();
        a1.put("1", "1");
        Map<String, Object> a12 = new HashMap<>();
        a12.put("2", "2");
        temp.computeIfAbsent("test", k -> new ArrayList<>()).add(a1);
        temp.computeIfAbsent("test", k -> new ArrayList<>()).add(a12);


        System.out.println(temp);


        System.out.println(Objects.hash("", "", "1"));
        System.out.println(Objects.hash("", "1", ""));
    }

    public static void main(String[] args) {
        Map<Pc0405MainKey, List<Pc0405SubTable>> mainTableMap = new HashMap<>();

        List<Map<String, Object>> dataList = generateDataList();
        dataList.forEach(item -> {
            String pipelineNumber = (String) item.get(PcmDataFieldConstant.PCM_PIPELINE_NUMBER);
            String device = (String) item.get(PcmDataFieldConstant.PCM_DEVICE);
            String unit = (String) item.get(PcmDataFieldConstant.PCM_UNIT);
            String prefabNo = (String) item.get(PcmDataFieldConstant.PCM_PREFAB_COMPONENTS_NO);
            String componentPath = MapUtils.getString(item, PcmDataFieldConstant.PCM_COMPONENT_PATH, StringUtils.EMPTY);

            // 判断是否存在对应key
            Pc0405MainKey key = new Pc0405MainKey(pipelineNumber, device, unit);
            List<Pc0405SubTable> pc0405SubTables = mainTableMap.computeIfAbsent(key, k -> new ArrayList<>());

            for (Pc0405MainKey keys : mainTableMap.keySet()) {
                if (keys.equals(key)) {
                    // 找到了 key 对象
                    key = keys;
                    break;
                }
            }
            // 更新 componentPath，只保留最小的路径
            if (StringUtils.isNotBlank(componentPath)) {
                if (key.getPcmComponentPath() == null || componentPath.compareTo(key.getPcmComponentPath()) < 0) {
                    key.setPcmComponentPath(componentPath);
                }
            }
            // 如果预制编号存在，构建子表
            if (prefabNo != null) {
                // 收集到当前主表下的所有预制件数据
                boolean exists = pc0405SubTables.stream().anyMatch(st -> st.getPrefabComponentsNo().equals(prefabNo));
                if (!exists) {
                    pc0405SubTables.add(new Pc0405SubTable(prefabNo, item));
                }
            }
        });
        System.out.println(mainTableMap);
    }

    /**
     * 生成符合业务逻辑的 dataList 测试数据
     *
     * @return List<Map < String, Object>>
     */
    public static List<Map<String, Object>> generateDataList() {
        List<Map<String, Object>> dataList = new ArrayList<>();

        // 示例数据 1
        Map<String, Object> data1 = new HashMap<>();
        data1.put(PcmDataFieldConstant.PCM_PIPELINE_NUMBER, "PIPE-001");
        data1.put(PcmDataFieldConstant.PCM_DEVICE, "DEVICE-A");
        data1.put(PcmDataFieldConstant.PCM_UNIT, "UNIT-101");
        data1.put(PcmDataFieldConstant.PCM_PREFAB_COMPONENTS_NO, "PREFAB-001");
        data1.put(PcmDataFieldConstant.PCM_COMPONENT_PATH, "333");
        dataList.add(data1);

        // 示例数据 2
        Map<String, Object> data2 = new HashMap<>();
        data2.put(PcmDataFieldConstant.PCM_PIPELINE_NUMBER, "PIPE-001");
        data2.put(PcmDataFieldConstant.PCM_DEVICE, "DEVICE-A");
        data2.put(PcmDataFieldConstant.PCM_UNIT, "UNIT-101");
        data2.put(PcmDataFieldConstant.PCM_PREFAB_COMPONENTS_NO, "PREFAB-002");
        data2.put(PcmDataFieldConstant.PCM_COMPONENT_PATH, "222");

        dataList.add(data2);

        // 示例数据 3
        Map<String, Object> data3 = new HashMap<>();
        data3.put(PcmDataFieldConstant.PCM_PIPELINE_NUMBER, "PIPE-002");
        data3.put(PcmDataFieldConstant.PCM_DEVICE, "DEVICE-B");
        data3.put(PcmDataFieldConstant.PCM_UNIT, "UNIT-102");
        data3.put(PcmDataFieldConstant.PCM_PREFAB_COMPONENTS_NO, "PREFAB-003");
        data3.put(PcmDataFieldConstant.PCM_COMPONENT_PATH, "333");
        dataList.add(data3);

        // 示例数据 4（无预制编号，用于测试 null 判断）
        Map<String, Object> data4 = new HashMap<>();
        data4.put(PcmDataFieldConstant.PCM_PIPELINE_NUMBER, "PIPE-002");
        data4.put(PcmDataFieldConstant.PCM_DEVICE, "DEVICE-B");
        data4.put(PcmDataFieldConstant.PCM_UNIT, "UNIT-102");
        // prefabNo 为 null，用于测试判断逻辑
        dataList.add(data4);

        return dataList;
    }

}
